package top.shiyiri.shops.controller;

import com.alibaba.fastjson.JSONObject;
import top.shiyiri.shops.bean.TimeCount;
import top.shiyiri.shops.service.TimeCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Aunean
 * @date 2021/12/27
 */
@Controller
public class TimeCountController {

    @Autowired
    private TimeCountService service;

    @RequestMapping("/timeAllCountByPage")
    @ResponseBody
    public String timeAllCountByPage(int page, HttpSession session) {
        List<TimeCount> list = service.timeAllCountByPage(page, 4);

        int lines = service.totalLines();

        int pages = lines%4 == 0 ? lines /4: lines /4 + 1;

        session.setAttribute("pages", pages);
        session.setAttribute("page", page);

        return JSONObject.toJSONString(list);
    }


    @RequestMapping("/getTimeCountPages")
    @ResponseBody
    public Integer getTimeCountPages(HttpSession session) {


        int pages = (Integer)session.getAttribute("pages");

        return pages;
    }

    @ResponseBody
    @RequestMapping("/timeAllCount")
    public String timeAllCount() {

        List<TimeCount> list = service.timeAllCount();

        return JSONObject.toJSONString(list);
    }
}
